package cn.eric.game.fujiatianxia6.po;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.DocumentException;

import cn.eric.game.fujiatianxia6.po.OneOnOne.FightData;
import cn.eric.game.fujiatianxia6.test.Dom4JforXML;

public class SkillFactory {

	private static List<Skill> skills;
	
	/**
	 * 
	* @Title: init
	* @Description: 初始化
	* @param     设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void init(){
		try {
			skills = Dom4JforXML.createSkills();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	public static List<Skill> getSkills() {
		return skills;
	}
	
	//根据ID返回技能
	public static Skill getSkillByID(String id){
		if("".equals(id) && id == null){
			return null;
		}
		for (Iterator iterator = skills.iterator(); iterator.hasNext();) {
			Skill skill = (Skill) iterator.next();
			if(skill.getId() == Integer.parseInt(id)){
				return skill;
			}
		}
		return null;
	}

	/**
	 * 
	* @Title: change
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param type 1 单挑 2 野战 3 攻城 4 
	* @param @param AttOrDef 1 进攻方  2 防守方
	* @param @param general 进攻方的英雄
	* @param @param virgin 未改变的
	* @param @return    设定文件
	* @return Object    返回类型
	* @throws
	 */
	public static Object changeBefore(int type, int AttOrDef, General general,General generalB,Object virgin) {
		Object result = new Object(); 
		if(type == 1){
			result = OneOnOne_Change(AttOrDef,general,virgin);
		}
		if(type == 2){
			result = BattleField_Change(AttOrDef,general,virgin);
		}
		return result;
	}

	// 野战修改
	private static Object BattleField_Change(int attOrDef, General general, Object virgin) {
		// TODO Auto-generated method stub
		BattleField BF = (BattleField) virgin;
		if("5".equals(general.getSkill())){  // 待伏
			System.out.println("武将："+general.getName()+"触发技能：待伏，发动伏兵，根据智力增加兵力");
			if(attOrDef == 1){
				BF.setAttackAmyNum((int)(BF.getAttackAmyNum() * (1+(float)((int)(Integer.parseInt(general.getIntelligence())))/2000)));
			}else{
				BF.setDefenceAmyNum((int)(BF.getDefenceAmyNum() * (1+(float)((int)(Integer.parseInt(general.getIntelligence())))/2000)));
			}
		}
		if("12".equals(general.getSkill()) && new Random().nextInt(100) <= SkillFactory.getSkillByID(general.getSkill()).getData()){  // 深谋
			System.out.println("武将："+general.getName()+"触发技能：深谋 野战或者攻城前，有几率直接对敌方造成伤害，伤害值与智力值有关，初始几率为15%，获得专属提升为30%（司马懿）");
			int lostNum = 0;
			if(attOrDef == 1){
				lostNum = (int) (BF.getAttackAmyNum() * (Float.parseFloat(general.getIntelligence()))/1000);
				System.out.println("防守方损失兵力：" + lostNum );
				BF.setDefenceAmyNum(BF.getDefenceAmyNum() - lostNum);
			}else{
				lostNum = (int) (BF.getDefenceAmyNum() * (Float.parseFloat(general.getIntelligence()))/1000);
				System.out.println("进攻方损失兵力：" + lostNum );
				BF.setAttackAmyNum(BF.getAttackAmyNum() - lostNum);
			}
		}
		return virgin;
	}

	// 野战修改
	private static Object BattleField_ChangeMiddle(int attOrDef, General general, Object virgin) {
		// TODO Auto-generated method stub
		BattleField BF = (BattleField) virgin;		
		if("2".equals(general.getSkill())){ // 奸雄
			if(new Random().nextInt(100) <= SkillFactory.getSkillByID(general.getSkill()).getData()){
				System.out.println("武将："+general.getName()+"触发技能：奸雄，野战或攻城时，若敌方所有武将加成后智力小于自己，敌方每回合有15%的几率陷入混乱，无法造成伤害，获得专属武器将会提升几率(30%)");
				if(attOrDef == 1){
					BF.setAttLost(0);
				}else{
					BF.setDefLost(0);
				}				
			}
		}
		if("6".equals(general.getSkill())){ // 大吼
			System.out.println("武将："+general.getName()+"触发技能：大吼，野战防守、守城时，额外增加进攻方的损失，根据武力值决定，获得专属武器，会额外提升损失的兵力");			
			// 增加最大10%的损失 专属后 最大15%
			int addLost = 0;
			if(attOrDef == 1){
				addLost = (int) (BF.getDefLost() * (Float.parseFloat(general.getAttack()))/1000);
				BF.setDefLost(BF.getDefLost() + addLost);
			}else{
				addLost = (int) (BF.getAttLost() * (Float.parseFloat(general.getAttack()))/1000);
				BF.setAttLost(BF.getAttLost() + addLost);
			}				
			System.out.println("增加伤亡：" + addLost);
		}
		if("14".equals(general.getSkill())){ // 坚守			
			int addLost = 0;
			if(attOrDef == 2){
				System.out.println("武将："+general.getName()+"触发技能：坚守 野战或者攻城防守中，极大的减少本队损失的人数，减少的额度与统帅值有关，最大20%");			
				addLost = (int) (BF.getDefLost() * (Float.parseFloat(general.getAttack()))/500);
				System.out.println("减少损失" + addLost + "(" + BF.getDefLost() + ")");
				BF.setDefLost(BF.getDefLost() - addLost);
			}				
		}
		if("15".equals(general.getSkill()) && new Random().nextInt(100) <= SkillFactory.getSkillByID(general.getSkill()).getData()){ // 火神
			System.out.println("武将："+general.getName()+"触发技能：火神 野战或者攻城中，有机率施展火攻术，造成极大伤害，威力值与统帅、武力值有关，初始几率为10%，获得专属提升为20%");			
			// 增加最大100%的损失
			int addLost = 0;
			if(attOrDef == 1){
				addLost = (int) (BF.getDefLost() * ((Float.parseFloat(general.getCommand()))/150 + (Float.parseFloat(general.getAttack()))/50));
				BF.setDefLost(BF.getDefLost() + addLost);
			}else{
				addLost = (int) (BF.getAttLost() * ((Float.parseFloat(general.getCommand()))/150 + (Float.parseFloat(general.getAttack()))/50));
				BF.setAttLost(BF.getAttLost() + addLost);
			}				
			System.out.println("增加伤亡至：" + addLost);
		}
		return BF;
	}

	
	// 单挑修改
	private static Object OneOnOne_Change(int attOrDef, General general, Object virgin) {
		//
		OneOnOne o = (OneOnOne)virgin;
		if("4".equals(general.getSkill())){ //单挑时对方防御力减少20%
			System.out.println("武将："+general.getName()+"触发技能：武圣，对方防御力减少百分之" + SkillFactory.getSkillByID(general.getSkill()).getData());
			if(attOrDef == 1){
				o.setDefenceB(o.getDefenceB() * (100 - SkillFactory.getSkillByID(general.getSkill()).getData())/100);
			}else{
				o.setDefenceA(o.getDefenceA() * (100 - SkillFactory.getSkillByID(general.getSkill()).getData())/100);
			}
		}
		return o;
	}
	
	/**
	 * @param oneOnOne 
	 * 
	* @Title: changeMiddle
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param type 1 单挑 2 野战 3 攻城 4 
	* @param @param AttOrDef 1 进攻方  2 防守方 3 所有一起算
	* @param @param generalA 进攻方的英雄
	* @param @param generalD 防御方的英雄
	* @param @param virgin 未改变的
	* @param @return    设定文件
	* @return Object    返回类型
	* @throws
	 */
	public static void changeMiddle(int type, int AttOrDef, General generalA, General generalD, Object virgin) {
		if(type == 1){
			FightData sigleFight = (FightData)virgin;
			OneOnOne_ChangeMiddle(AttOrDef,generalA,generalD,sigleFight);
		}
		if(type == 2){ // 如果是野战要注意把6个武将的技能都照顾到
			// 如果有神算触发，则另一方的技能不能触发
			BattleField BF= (BattleField)virgin;

			if( (BF.getAttackChief() != null && "13".equals(BF.getAttackChief().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackChief().getSkill()).getData())) 
					|| (BF.getAttackVice() != null && "13".equals(BF.getAttackVice().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackVice().getSkill()).getData()))
					|| (BF.getAttackCounsellor() != null && "13".equals(BF.getAttackCounsellor().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackCounsellor().getSkill()).getData()))){ //神算 触发几率
				// 只有进攻方的技能触发
				System.out.println("神算技能触发，防守方无法释放技能");
				battleField_Middle_Attack(BF);
			}
			if( (BF.getAttackChief() != null && "13".equals(BF.getAttackChief().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackChief().getSkill()).getData())) 
					|| (BF.getAttackVice() != null && "13".equals(BF.getAttackVice().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackVice().getSkill()).getData()))
					|| (BF.getAttackCounsellor() != null && "13".equals(BF.getAttackCounsellor().getSkill()) 
					&& (new Random().nextInt(101) <= SkillFactory.getSkillByID(BF.getAttackCounsellor().getSkill()).getData()))){ //神算 触发几率
				// 只有进攻方的技能触发
				System.out.println("神算技能触发，进攻方无法释放技能");
				battleField_Middle_Defence(BF);
			}
			
			// 没有神算
			battleField_Middle_Attack(BF);
			battleField_Middle_Defence(BF);
		}
	}

	private static void battleField_Middle_Defence(BattleField BF) {
		if(BF.getDefenceChief() != null){
			BattleField_ChangeMiddle(2,BF.getDefenceChief(),BF);				
		}
		if(BF.getDefenceVice() != null){
			BattleField_ChangeMiddle(2,BF.getDefenceVice(),BF);				
		}
		if(BF.getDefenceCounsellor() != null){
			BattleField_ChangeMiddle(2,BF.getDefenceCounsellor(),BF);				
		}
	}

	private static void battleField_Middle_Attack(BattleField BF) {
		if(BF.getAttackChief() != null){
			BattleField_ChangeMiddle(1,BF.getAttackChief(),BF);				
		}
		if(BF.getAttackVice() != null){
			BattleField_ChangeMiddle(1,BF.getAttackVice(),BF);				
		}
		if(BF.getAttackCounsellor() != null){
			BattleField_ChangeMiddle(1,BF.getAttackCounsellor(),BF);				
		}
	}

	// 单挑中修改
	private static void OneOnOne_ChangeMiddle(int attOrDef, General generalA, General generalD, FightData sigleFight) {
		
		// 鬼将
		if("7".equals(generalA.getSkill()) && sigleFight.attackType == 1){//鬼将 单挑时，普通攻击造成1.2倍的伤害 1表示普通攻击
			System.out.println("武将" + generalA.getName() + "技能：" + SkillFactory.getSkillByID(generalA.getSkill()).getName() + "触发,造成" + SkillFactory.getSkillByID(generalA.getSkill()).getData() +  "%的伤害");
			sigleFight.defenceLostHealth = sigleFight.defenceLostHealth * SkillFactory.getSkillByID(generalA.getSkill()).getData() / 100;
		}
		if("7".equals(generalD.getSkill()) && sigleFight.defenceType == 1){//鬼将 单挑时，普通攻击造成1.2倍的伤害 1表示普通攻击
			System.out.println("武将" + generalD.getName() + "技能：" + SkillFactory.getSkillByID(generalD.getSkill()).getName() + "触发,造成" + "%的伤害");
			sigleFight.attackLostHealth = sigleFight.attackLostHealth * SkillFactory.getSkillByID(generalD.getSkill()).getData() / 100;
		}
		
		// 猛将
		if("11".equals(generalA.getSkill()) && new Random().nextInt(101) <= SkillFactory.getSkillByID(generalA.getSkill()).getData()){//鬼将 单挑时，普通攻击造成1.2倍的伤害 1表示普通攻击
			System.out.println("武将" + generalA.getName() + "技能：" + SkillFactory.getSkillByID(generalA.getSkill()).getName() + "触发,额外造成" + SkillFactory.getSkillByID(generalA.getSkill()).getData() +  "%的伤害");
			sigleFight.defenceLostHealth = sigleFight.defenceLostHealth * (SkillFactory.getSkillByID(generalA.getSkill()).getData() + 100) / 100;
		}
		if("11".equals(generalD.getSkill()) && new Random().nextInt(101) <= SkillFactory.getSkillByID(generalA.getSkill()).getData()){//鬼将 单挑时，普通攻击造成1.2倍的伤害 1表示普通攻击
			System.out.println("武将" + generalD.getName() + "技能：" + SkillFactory.getSkillByID(generalD.getSkill()).getName() + "触发,额外造成" + SkillFactory.getSkillByID(generalA.getSkill()).getData() +  "%的伤害");
			sigleFight.attackLostHealth = sigleFight.attackLostHealth * (SkillFactory.getSkillByID(generalD.getSkill()).getData() + 100) / 100;
		}
		
	}

	/**
	 * 
	* @Title: changeAfter
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param type 1 单挑 2 野战 3 攻城  4 俘虏后
	* @param @param AttOrDef 1 进攻方  2 防守方 3 所有一起算
	* @param @param generalA 进攻方的英雄
	* @param @param generalD 防御方的英雄
	* @param @param virgin 未改变的
	* @param @return    设定文件
	* @return Object    返回类型
	* @throws
	 */
	public static void changeAfter(int type, int AttOrDef, General generalA, General generalD, Object virgin) {
		if(type == 1){
			OneOnOne fight = (OneOnOne)virgin;
			OneOnOne_ChangeAfter(AttOrDef,generalA,generalD,fight);
		}
		
		if(type == 4){ // 俘虏后，判定技能
			beCatched(generalA,generalD);
		}
		
	}
	
	private static void beCatched(General lord, General lost) {
		if("1".equals(lord.getSkill())){  //仁德 
			System.out.println("武将" + lord.getName() + "技能：" + SkillFactory.getSkillByID(lord.getSkill()).getName() + "触发,武将直接加入阵营");
			lost.setBelongTo(lord.getId());
			lost.setStatus("0");
			System.out.println(lost.getName() + "拜入" + lord.getName() + "帐下");
		}		
	}

	// 单挑每回合结束时触发
	private static void OneOnOne_ChangeAfter(int attOrDef, General generalA, General generalD, OneOnOne fight) {
		// 裸衣
		if("9".equals(generalA.getSkill())){//裸衣 
			int addAtt = (100 - fight.getVitalityA()) * SkillFactory.getSkillByID(generalA.getSkill()).getData() / 100;
			System.out.println("武将" + generalA.getName() + "技能：" + SkillFactory.getSkillByID(generalA.getSkill()).getName() + "触发,增加" + addAtt +  "攻击力");
			fight.setAttackA(fight.getAttackA() + addAtt);
		}
		if("9".equals(generalD.getSkill())){//鬼将 单挑时，普通攻击造成1.2倍的伤害 1表示普通攻击
			int addAtt = (100 - fight.getVitalityB()) * SkillFactory.getSkillByID(generalD.getSkill()).getData() / 100;
			System.out.println("武将" + generalD.getName() + "技能：" + SkillFactory.getSkillByID(generalD.getSkill()).getName() + "触发,增加" + addAtt +  "攻击力");
			fight.setAttackB(fight.getAttackB() + addAtt);			
		}
		
		// 恶来
		if("10".equals(generalA.getSkill())){//恶来
			int addDef = (100 - fight.getVitalityA()) * SkillFactory.getSkillByID(generalA.getSkill()).getData() / 100;
			System.out.println("武将" + generalA.getName() + "技能：" + SkillFactory.getSkillByID(generalA.getSkill()).getName() + "触发,增加" + addDef +  "防御力");
			fight.setDefenceA(fight.getDefenceA() + addDef);
		}
		if("10".equals(generalD.getSkill())){//恶来
			int addDef = (100 - fight.getVitalityB()) * SkillFactory.getSkillByID(generalD.getSkill()).getData() / 100;
			System.out.println("武将" + generalD.getName() + "技能：" + SkillFactory.getSkillByID(generalD.getSkill()).getName() + "触发,增加" + addDef +  "防御力");
			fight.setDefenceB(fight.getDefenceB() + addDef);			
		}		
		
	}
}
